package com.bigdata.service;

import com.bigdata.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //1.从请求中取出添加的学生信息
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");


        String stu_id = request.getParameter("sid");
        String stu_name = request.getParameter("sname");
        String stu_gender = request.getParameter("gender");
        String stu_date = request.getParameter("date");
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.得到连接对象
            conn = JDBCUtils.getConnection();
            String sql = "insert into student values (?,?,?,?)";
            //2.创建ps对象
            ps = conn.prepareStatement(sql);
            ps.setString(1, stu_id);
            ps.setString(2, stu_name);
            ps.setString(3, stu_gender);
            ps.setString(4, stu_date);
            //3.执行操作
            int i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn, ps);
            System.out.println("学生添加成功！！！");
        }





    }
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            doGet(request, response);
        }
    }